# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：asyncio_requests.py
#日期：2019-09-03
#备注：并发http请求怎么办呢，通常是用requests，但requests是同步的库，如果想异步的话需要引入aiohttp    
      pip install aiohttp
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from pycacho.cachobase.deal_concurrent import thread_task,asyncio_task,process_task,gevent_task
import time
from aiohttp import ClientSession
import requests
from interfacetest.baseset.appsvr_api import API_APPSVR

url = 'https://test.zxtouch.msxiaodai.com'
headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json'
        }


def user_info(i):
    fu_url = url + '/app/userInfo'
    data = API_APPSVR['submission']
    data['usrNo'] = 'YH20191119000001'
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    print(data, i)


def single_task(t):
    sTime = time.time()
    for i in range(1,t+1):
        user_info(i)
    eTime = time.time()
    s = eTime - sTime
    print('花费的时间为：%.2f秒' % (s))


async def async_info(i):
    fu_url = url + '/app/userInfo'
    data = API_APPSVR['submission']
    data['usrNo'] = 'YH20191119000001'
    async with ClientSession() as session:
        async with session.post(fu_url, json=data, headers=headers) as response:
            resp = await response.text()
            print(resp,i)


if __name__ == '__main__':
    t = 100
    #asyncio_task(async_info,t)   # 0.8秒
    thread_task(user_info,t)      # 2~3秒
    #gevent_task(user_info, t)    # 2~3秒
    #process_task(user_info,t)    # 40秒
    #single_task(t)               # 7~8秒


